package com.lagou.dao.impI;

import com.lagou.dao.AccountDao;
import com.lagou.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
 * @Author wsl
 * @Date 2021/11/10 20:57
 * @Project_Name jdbc_spring
 */
@Repository("accountDao") //生成该类实例存到IOC容器中
public class AccountDaoImpI implements AccountDao {

    @Autowired
    private QueryRunner queryRunner;

    @Autowired
    private ConnectionUtils connectionUtils;

    /**
     * 转出操作
     * @param outUser
     * @param money
     */
    @Override
    public void out(String outUser, Double money) {

        String sql = "update account set money = money - ? where name = ?";
        try {
            queryRunner.update(connectionUtils.getThreadConnection(),sql,money,outUser);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 转入操作
     * @param inUser
     * @param money
     */
    @Override
    public void in(String inUser, Double money) {

        String sql = "update account set money = money + ? where name = ?";
        try {
            queryRunner.update(connectionUtils.getThreadConnection(),sql,money,inUser);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
